package com.onedec.account.controller;

import com.onedec.account.entity.OnedecAccount;
import com.onedec.account.entity.OnedecAccountingVouchers;
import com.onedec.account.service.FileService;
import com.onedec.account.service.OnedecAccountService;
import com.onedec.account.service.OnedecAccountingVouchersService;
import com.onedec.account.util.Page;
import com.onedec.account.util.Result;
import com.onedec.utils.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("account")

public class OnedecAccountController {
    @Autowired
    private OnedecAccountService onedecAccountService;

    @Autowired
    private OnedecAccountingVouchersService onedecAccountingVouchersService;
    @Autowired
    FileService fileService;

    @Value("${baseUploadUrl}")
    private String url;

    @RequestMapping("query")
        public Result selectCategory(OnedecAccount onedecAccount,Page page, int num) throws Exception {
        HashMap<String, Object> map = new HashMap<String,Object>();
        map.put("accountSerialNum", onedecAccount.getAccountSerialNum());
        map.put("accountTotal", onedecAccount.getAccountTotal());
        map.put("pageIndex",page.getPageIndex());
        map.put("num",num);
        map.put("accountRemark", onedecAccount.getAccountRemark());
        long dataCount=onedecAccountService.accountnum();
        System.out.println(dataCount);
        page.setTotalCount(dataCount);
        page.setTotalPage((dataCount-1)/num+1);
        System.out.println(onedecAccountService.findAll().toString());
        List<Map<String, Object>> maps = onedecAccountService.selectByMap(map);

        return new Result("200", "success", page, maps);
    }



    @RequestMapping("deleteAll")
    public  Result getAway(@RequestParam(value="nums[]") Integer[] nums) throws Exception {
        for (Integer i : nums) {
            onedecAccountService.deleteById(i);
        }
        return new Result("200","success",null,null);
    }
    //单独删除一个商品
    @RequestMapping("delete")
    public Result delete( int id) throws Exception {
        onedecAccountService.deleteById(id);
        return new Result("200","success",null,null);
    }


    @RequestMapping("selectById")
    public Result selectById(int id) throws Exception {
        OnedecAccount onedecAccount = onedecAccountService.selectById(id);
        List<OnedecAccountingVouchers> list = onedecAccountingVouchersService.selectByAccountId(id);
        return new Result("200","success",onedecAccount,list);
    }

    @RequestMapping("update")
    public Result update(@Validated OnedecAccount onedecAccount, BindingResult result) throws Exception {
        System.out.println(onedecAccount.getAccountRemark()+"..........."+onedecAccount.getAccountSerialNum()+"..........."+onedecAccount.getAccountOperationNum());
        if(result.hasErrors()){
            throw new  RuntimeException("参数错误");
        }

        onedecAccountService.updateOne(onedecAccount);
        return new Result("200","success",null,null);
}
    @RequestMapping("updatepic")
    @ApiOperation(value = "单个图片上传到七牛云")
    public Result updatepic(@RequestParam(value = "pic") MultipartFile upfile,int id) throws Exception {

            String fileName = upfile.getOriginalFilename();
            File file = new File(url + fileName);
            try{
                //将MulitpartFile文件转化为file文件格式
                upfile.transferTo(file);
                Map response = fileService.uploadFile(file);
                Object imageName = response.get("imgName");
                String picUrl="http://qddyllf7e.bkt.clouddn.com/"+imageName;
                OnedecAccountingVouchers onedecAccountingVouchers = onedecAccountingVouchersService.selectById(id);
                onedecAccountingVouchers.setAccountingProofOfAddress(picUrl);
                onedecAccountingVouchersService.update(onedecAccountingVouchers);
               int newid=onedecAccountingVouchers.getAccountingEntryId();
                return new Result("200","success",newid,null);
            }catch (Exception e){
                e.printStackTrace();
                return new Result("1000","error",null,null);        }

    }



    @Transactional
    @RequestMapping("insert")
    @ApiOperation(value = "多个图片上传到七牛云")
    public Result insert(@Validated OnedecAccount onedecAccount, BindingResult result,@RequestParam(value = "img") MultipartFile[] upfile)throws Exception {
        if(result.hasErrors()){
            throw new  RuntimeException("参数错误");
        }
        System.out.println(onedecAccount+" ..."+onedecAccount.getAccountRemark());
        onedecAccountService.insertOne(onedecAccount);
        //取id存到入账凭证表
        int id = onedecAccount.getAccountId();
        for (MultipartFile files:upfile) {
            String fileName = files.getOriginalFilename();
            File file = new File(url + fileName);
            try{
                //将MulitpartFile文件转化为file文件格式
                files.transferTo(file);
                Map response = fileService.uploadFile(file);
                Object imageName = response.get("imgName");
                String picUrl="http://qddyllf7e.bkt.clouddn.com/"+imageName;
                OnedecAccountingVouchers onedecAccountingVouchers=new OnedecAccountingVouchers();
                onedecAccountingVouchers.setAccountingEntryId(id);
                onedecAccountingVouchers.setAccountingProofOfAddress(picUrl);
                onedecAccountingVouchersService.insert(onedecAccountingVouchers);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return new Result("200","success",null,null);
    }
}
